﻿using Function;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace UI.Module.Statistical.View
{
    public partial class Index : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPage();
                if (!string.IsNullOrEmpty(Request.QueryString["shopid"]))
                {
                    ddlistShop.SelectedValue = Request.QueryString["shopid"];
                }
            }
        }
        private void LoadPage()
        {
            int RecordCount = 0;
            DataTable Page_Dt = new DataTable();
            Page_Dt = BLL.PageQuery2005_BLL.SelectPage(out RecordCount, "ShopID,(select STitle from dbo.ST_Shop where id=ShopID)as STitle", "GX_InvestorShop", " InvestorID=" + Method.CurrentUserID, "[ID] desc", PubConstant.DataBaseTarget.JHShop, out Message);
            ddlistShop.DataSource = Page_Dt;
            ddlistShop.DataTextField = "STitle";
            ddlistShop.DataValueField = "ShopID";
            ddlistShop.DataBind();
            //ddlistShop.Items.Insert(0, new ListItem() { Value = "0", Text = "--所有门店--" });
        }
        string Message = "";
        private void Bind(string shopid = "", bool issearch = false)
        {
            if (issearch)
            {
                Pager.CurrentPageIndex = 1;
            }
            int RecordCount = 0;
            DataTable Page_Dt = new DataTable();
            Page_Dt = BLL.JsonBLL.SelectPage(out RecordCount, ddlistType.SelectedItem.Value.ToInt32(), string.IsNullOrEmpty(shopid) ? ddlistShop.SelectedItem.Value.ToInt32() : shopid.ToInt32(), Pager.PageSize, Pager.CurrentPageIndex, PubConstant.DataBaseTarget.JHShop, out Message);

            if (!string.IsNullOrEmpty(Message))
            {
                MessageBox box = new MessageBox();
                box.ErrorBox(this, "数据查询失败!<br/>" + Message);
                return;
            }
            grdlist.DataSource = Page_Dt;
            grdlist.DataBind();
            this.Pager.RecordCount = RecordCount;
        }
        protected void Pager_BindData(object sender, EventArgs e)
        {
            Bind(Request.QueryString["shopid"]);
        }
        protected string GetDetailed()
        {
            string str = "";
            return str;
        }
        protected void grdlist_RowCreated(object sender, GridViewRowEventArgs e)
        {
            //创建多行表头
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //获取表头所在行的所有单元格  
                TableCellCollection tcHeader = e.Row.Cells;
                //清除自动生成的表头  
                tcHeader.Clear();

                tcHeader.Add(new TableHeaderCell());
                tcHeader[0].RowSpan = 2;
                tcHeader[0].Text = "统计时间";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[1].ColumnSpan = 6;
                tcHeader[1].Text = "零售";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[2].ColumnSpan = 6;
                tcHeader[2].Text = "大宗";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[3].ColumnSpan = 6;
                tcHeader[3].Text = "推送";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[4].ColumnSpan = 2;
                tcHeader[4].Text = "临时";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[5].ColumnSpan = 5;
                tcHeader[5].Text = "合计</th></tr><tr>";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[6].Text = "订单数";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[7].Text = "总金额";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[8].Text = "总收益";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[9].Text = "退货单数";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[10].Text = "退货单总金额";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[11].Text = "退货单总支出";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[12].Text = "订单数";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[13].Text = "总金额";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[14].Text = "总收益";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[15].Text = "退货单数";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[16].Text = "退货单总金额";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[17].Text = "退货单总支出";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[18].Text = "订单数";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[19].Text = "总金额";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[20].Text = "总收益";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[21].Text = "退货单数";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[22].Text = "退货单总金额";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[23].Text = "退货单总支出";

                tcHeader.Add(new TableHeaderCell());
                tcHeader[24].Text = "订单数";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[25].Text = "总金额";


                tcHeader.Add(new TableHeaderCell());
                tcHeader[26].Text = "总金额";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[27].Text = "收益合计";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[28].Text = "退单总金额";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[29].Text = "支出合计";
                tcHeader.Add(new TableHeaderCell());
                tcHeader[30].Text = "最终收益</th></tr><tr>";
            }
        }

        protected void ddlistShop_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void ddlistType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind();
        }
    }
}